layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table'], function () {
    var form = layui.form,
        table = layui.table,
        admin = layui.admin;

    table.render({
        elem: '#company_table'
        , url: '/company/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', title: 'companyID', width: 120, "align": "center"}
            , {field: 'name', width: 200, title: '公司名称', "align": "center"}
            , {field: 'address', width: 200, title: '地址', "align": "center"}
            , {field: 'mobile', width: 200, title: '手机号码', "align": "center"}
            , {field: 'userName', width: 180, title: '登录用户名', "align": "center"}
            , {field: 'createdTime', width: 200, title: '创建时间', "align": "center"}
            , {
                field: 'OPT', width: 160, title: '操作', "align": "center", templet: function (d) {
                    return '<a  href="#" class="layui-btn layui-btn-xs" lay-event="modify_company" >编辑</a>';
                }
            }
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {}
        , id: 'company_table'
        , page: true
    });



    table.on('tool(company_table)', function (obj) {
        var table_event = obj.event;

        var data = obj.data;

        if (table_event == "modify_company") {
            admin.popup({
                title: '编辑'
                , shade: 0
                , anim: -1
                , offset: "20px"
                , area: ['80%', '500px']
                , id: 'layadmin-layer-skin-modify'
                , skin: 'layui-anim layui-anim-upbit'
                , content: $("#add_company_win")
                , end: function () {
                    table.reload('company_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            name: $("#company_name").val(),
                        }
                    });
                }
            });
            form.val('add_company_form', {
                "id": data.id,
                "userId": data.userId,
                "name": data.name,
                "address": data.address,
                "mobile": data.mobile,
                "userName": data.userName
            })
        }

    });

    //添加company
    $(".add_btn").click(function () {

        $("#add_company_form")[0].reset();
        admin.popup({
            title: '编辑'
            , shade: 0
            , anim: -1
            , offset: "20px"
            , area: ['80%', '500px']
            , id: 'layadmin-layer-skin-modify'
            , skin: 'layui-anim layui-anim-upbit'
            , content: $("#add_company_win")
            , end: function () {
                table.reload('company_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        name: $("#company_name").val(),
                    }
                });
            }
        });
    });

    $("#company_search_btn").click(function () {
        table.reload('company_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                name: $("#company_name").val(),
            }
        });
    });


    /**
     * oem 新增提交
     */
    form.on('submit(addOk)', function (data) {
        $.ajax({
            url: "/company/editCompany",
            data: data.field,
            type: "post",
            success: function (result) {
                if (result.code == 1) {
                    layer.msg("操作成功", {
                        offset: '32%'
                        , icon: 1
                        , time: 3000
                        , btn: '知道了'
                        , btnAlign: "c"
                        , shade: 0.4
                        , yes: function () {
                            table.reload('company_table', {});
                            layer.closeAll();
                        }
                    })
                } else {
                    layer.msg(result.message, {
                        offset: '32%'
                        , icon: 2
                        , time: 3000
                    }, function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });
})

